﻿/*
 * @file: CartManager.cs
 * @in namespace: Cart
 */

using System;
using System.Collections.Generic;
using System.Web;

namespace Cart
{
    public class CartManager
    {
        public List<CartItem> Items { get; set; }
        public static readonly CartManager Instance;

        /// <summary>
        /// get or create new shopping cart items
        /// </summary>
        static CartManager()
        {
            if (HttpContext.Current.Session["ShoppingCart"] == null)
            {
                Instance = new CartManager();
                Instance.Items = new List<CartItem>();
                HttpContext.Current.Session["ShoppingCart"] = Instance;
            }
            else
                Instance = HttpContext.Current.Session["ShoppingCart"] as CartManager;
        }

        /// <summary>
        /// insert new item to shopping cart
        /// </summary>
        /// <param name="bouquetID">Bouquet ID</param>
        public void AddItem(int bouquetID)
        {
            try
            {
                CartItem newItem = new CartItem(bouquetID);
                if (newItem.BouquetName != null)
                {
                    if (Items.Contains(newItem))
                    {
                        foreach (CartItem currentItem in Items)
                        {
                            if (currentItem.Equals(newItem))
                            {
                                currentItem.Quantity++;
                                break;
                            }
                        }
                    }
                    else
                    {
                        newItem.Quantity = 1;
                        Items.Add(newItem);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// remove item from shopping cart
        /// </summary>
        /// <param name="bouquetID">Bouquet ID</param>
        public void RemoveItem(int bouquetID)
        {
            try
            {
                CartItem removeItem = new CartItem(bouquetID);
                Items.Remove(removeItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// change Bouquet quantity in shoppingcart
        /// </summary>
        /// <param name="bouquetID">Bouquet ID</param>
        /// <param name="quantity">Bouquet quantity</param>
        public void SetItemQuantity(int bouquetID, int quantity)
        {
            try
            {
                if (quantity == 0)
                    RemoveItem(bouquetID);
                else
                {
                    CartItem updateItem = new CartItem(bouquetID);
                    foreach (CartItem currentItem in Items)
                    {
                        if (currentItem.Equals(updateItem))
                        {
                            currentItem.Quantity = quantity;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// return sub total for each items in shopping cart
        /// </summary>
        /// <returns>Bouquet sub total </returns>
        public decimal GetSubTotal()
        {
            decimal result = 0;
            foreach (CartItem currentItem in Items)
            {
                result += currentItem.TotalPrice;
            }
            return result;
        }

        protected CartManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}